package cn.iocoder.yudao.module.shop.controller.admin.agent;


import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.agent.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.agent.AgentDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.agent.ManageDO;
import cn.iocoder.yudao.module.shop.service.agent.AgentService;

@Tag(name = "管理后台 - 店铺代理商")
@RestController
@RequestMapping("/shop/agent")
@Validated
public class AgentController {

    @Resource
    private AgentService agentService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺代理商")
    @PreAuthorize("@ss.hasPermission('shop:agent:create')")
    public CommonResult<Long> createAgent(@Valid @RequestBody AgentSaveReqVO createReqVO) {
        return success(agentService.createAgent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺代理商")
    @PreAuthorize("@ss.hasPermission('shop:agent:update')")
    public CommonResult<Boolean> updateAgent(@Valid @RequestBody AgentSaveReqVO updateReqVO) {
        agentService.updateAgent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店铺代理商")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:agent:delete')")
    public CommonResult<Boolean> deleteAgent(@RequestParam("id") Long id) {
        agentService.deleteAgent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺代理商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:agent:query')")
    public CommonResult<AgentRespVO> getAgent(@RequestParam("id") Long id) {
        AgentDO agent = agentService.getAgent(id);
        return success(BeanUtils.toBean(agent, AgentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺代理商分页")
    @PreAuthorize("@ss.hasPermission('shop:agent:query')")
    public CommonResult<PageResult<AgentRespVO>> getAgentPage(@Valid AgentPageReqVO pageReqVO) {
        PageResult<AgentDO> pageResult = agentService.getAgentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AgentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺代理商 Excel")
    @PreAuthorize("@ss.hasPermission('shop:agent:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAgentExcel(@Valid AgentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AgentDO> list = agentService.getAgentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店铺代理商.xls", "数据", AgentRespVO.class,
                        BeanUtils.toBean(list, AgentRespVO.class));
    }

    // ==================== 子表（店铺管理） ====================

    @GetMapping("/manage/page")
    @Operation(summary = "获得店铺管理分页")
    @PreAuthorize("@ss.hasPermission('shop:agent:query')")
    public CommonResult<PageResult<ManageDO>> getManagePage(PageParam pageReqVO, Long agentId) {
        return success(agentService.getManagePage(pageReqVO,agentId));
    }

    @PostMapping("/manage/create")
    @Operation(summary = "创建店铺管理")
    @PreAuthorize("@ss.hasPermission('shop:agent:create')")
    public CommonResult<Long> createManage(@Valid @RequestBody ManageDO manage) {
        return success(agentService.createManage(manage));
    }

    @PutMapping("/manage/update")
    @Operation(summary = "更新店铺管理")
    @PreAuthorize("@ss.hasPermission('shop:agent:update')")
    public CommonResult<Boolean> updateManage(@Valid @RequestBody ManageDO manage) {
        agentService.updateManage(manage);
        return success(true);
    }

    @DeleteMapping("/manage/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除店铺管理")
    @PreAuthorize("@ss.hasPermission('shop:agent:delete')")
    public CommonResult<Boolean> deleteManage(@RequestParam("id") Long id) {
        agentService.deleteManage(id);
        return success(true);
    }

	@GetMapping("/manage/get")
	@Operation(summary = "获得店铺管理")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:agent:query')")
	public CommonResult<ManageDO> getManage(@RequestParam("id") Long id) {
	    return success(agentService.getManage(id));
	}

}